# main.py
import queue
import logging
import time

from logger import Logger
from camera import Camera
from streamer import Streamer


def main():
    # 初始化日志，设置日志等级（比如 debug、trace、spam）
    Logger("debug")

    cam = Camera()
    streamer = Streamer()

    url = (
        'udpsrc address=230.1.1.1 port=1720 multicast-iface="eth0" '
        "! application/x-rtp, media=video, encoding-name=H264 "
        "! rtph264depay ! h264parse ! avdec_h264 ! videoconvert "
        "! video/x-raw,width=1280,height=720,format=BGR ! appsink drop=1"
    )
    if not cam.open(url):
        print("Failed to open camera")
        return

    frame_queue = queue.Queue(maxsize=30)
    cam.output_queue_set(frame_queue)
    streamer.input_queue_set(frame_queue)

    # ffplay -fflags nobuffer -flags low_delay -framedrop -strict experimental -infbuf rtmp://192.168.123.100/live/stream
    streamer.open("rtmp://192.168.123.100/live/stream")

    cam.start()
    streamer.start()

    print("Press Ctrl+C to stop")

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        print("Stopping...")

    cam.stop()
    streamer.stop()

    cam.close()
    streamer.close()


if __name__ == "__main__":
    main()
